/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.controller.dashboard;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.SMSMailUtil;
import br.com.jr2tecnologia.model.BankAccountPersistenceLocal;
import br.com.jr2tecnologia.model.CreditPersistenceLocal;
import br.com.jr2tecnologia.model.entity.BankAccount;
import br.com.jr2tecnologia.model.entity.Credit;
import br.com.jr2tecnologia.model.entity.Login;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.model.SelectItem;

/**
 *
 * @author ronildo
 */
@ManagedBean
@SessionScoped
public class FinancesDashboardMBean implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger();
    private Login loginInSession;
    
    @EJB
    private BankAccountPersistenceLocal bankAccountPersistence;
    @EJB
    private CreditPersistenceLocal creditPersistence;

    private List<BankAccount> listBankAccount;
    private List<Credit> listCredit;

    private String optionMenuBankAccount;
    private BankAccount selectedBankAccount;
    private BankAccount createBankAccount;
    private Credit selectedCredit;
    private Credit createCredit;

    /** Creates a new instance of FinancesDashboard */
    public FinancesDashboardMBean() {
        loginInSession = SMSMailUtil.getLoginInSession();
        createBankAccount = new BankAccount();
        selectedBankAccount =  new BankAccount();
        createCredit = new Credit();
        selectedCredit = new Credit();
    }

    @PostConstruct
    public void init(){
        searchBankAccount();
        searchCredit();
    }

    public List<BankAccount> getListBankAccount() {
        return listBankAccount;
    }

    public List<Credit> getListCredit() {
        return listCredit;
    }

    public BankAccount getCreateBankAccount() {
        return createBankAccount;
    }

    public void setCreateBankAccount(BankAccount createBankAccount) {
        this.createBankAccount = createBankAccount;
    }

    public BankAccount getSelectedBankAccount() {
        return selectedBankAccount;
    }

    public void setSelectedBankAccount(BankAccount selectedBankAccount) {
        this.selectedBankAccount = selectedBankAccount;
    }

    public Credit getCreateCredit() {
        return createCredit;
    }

    public void setCreateCredit(Credit createCredit) {
        this.createCredit = createCredit;
    }

    public Credit getSelectedCredit() {
        return selectedCredit;
    }

    public void setSelectedCredit(Credit selectedCredit) {
        this.selectedCredit = selectedCredit;
    }

    public String getOptionMenuBankAccount() {
        return optionMenuBankAccount;
    }

    public void setOptionMenuBankAccount(String optionMenuBankAccount) {
        this.optionMenuBankAccount = optionMenuBankAccount;
    }

    private void searchBankAccount(){
        if(loginInSession.getLoginRulesView().isRoot())
            listBankAccount = bankAccountPersistence.listAllBankAccount();
        else
            listBankAccount = bankAccountPersistence.listBankAccountsOfCompany(loginInSession.getCompany());
    }

    private void searchCredit(){
        if(loginInSession.getLoginRulesView().isRoot())
            listCredit = bankAccountPersistence.listAllCredit();
        else
            listCredit = bankAccountPersistence.listCreditsOfCompany(loginInSession.getCompany());
    }

    public List<SelectItem> getMenuBankAccount() {
        List<SelectItem> menu = new ArrayList<SelectItem>();

        menu.add(new SelectItem(0, ""));
        for(int cont = 1, index = 0; index < listBankAccount.size(); cont++, index++){
            menu.add(new SelectItem(cont, listBankAccount.get(index).getName()));
        }
        return menu;
    }

    public void createBankAccount(){
        createBankAccount.setCompany(loginInSession.getCompany());
        bankAccountPersistence.persist(createBankAccount);
        SMSMailUtil.putFacesMessage("Sucesso", "Conta "+createBankAccount.getName()+" criada com sucesso");
        searchBankAccount();
    }

    public void updateBankAccount(){
        bankAccountPersistence.update(selectedBankAccount);
        SMSMailUtil.putFacesMessage("Sucesso", "Conta "+selectedBankAccount.getName()+" atualizada com sucesso");
        searchBankAccount();
        searchCredit();
    }

    public void removeBankAccount(){
        bankAccountPersistence.remove(selectedBankAccount);
        SMSMailUtil.putFacesMessage("Sucesso", "Conta "+selectedBankAccount.getName()+" removida com sucesso");
        searchBankAccount();
        searchCredit();
    }

    public void recalculateSms(){
        bankAccountPersistence.recalculateQuantitySmsBuyed(selectedBankAccount);
        SMSMailUtil.putFacesMessage("Sucesso", "Conta "+selectedBankAccount.getName()+" recalculada com sucesso");
        searchBankAccount();
    }

    public void recalculateBalance(){
        bankAccountPersistence.recalculateBalance(selectedBankAccount);
        SMSMailUtil.putFacesMessage("Sucesso", "Conta "+selectedBankAccount.getName()+" recalculada com sucesso");
        searchBankAccount();
    }

    public void createCredit(){
        if(Integer.parseInt(optionMenuBankAccount) > 0){
            createCredit.setBankAccount(listBankAccount.get(Integer.parseInt(optionMenuBankAccount)-1));
            createCredit.setDatetimeRegistered(new Date());
            creditPersistence.persist(createCredit);
            SMSMailUtil.putFacesMessage("Sucesso", "Credito de "+createCredit.getValue()+" criada com sucesso");
            searchBankAccount();
            searchCredit();
        }else
            SMSMailUtil.putFacesMessage("Erro", "Selecione uma conta para debitar o crédito");
    }

    public void updateCredit(){
        creditPersistence.update(selectedCredit);
        SMSMailUtil.putFacesMessage("Sucesso", "Credito de "+selectedCredit.getValue()+" atualizado com sucesso");
        searchBankAccount();
        searchCredit();
    }

    public void removeCredit(){
        creditPersistence.remove(selectedCredit);
        SMSMailUtil.putFacesMessage("Sucesso", "Credito de "+selectedCredit.getValue()+" removido com sucesso");
        searchBankAccount();
        searchCredit();
    }
}
